package route

import (
	"fmt"
	"net/http"
	"pdocker/docker"
	"pdocker/task"
	"pdocker/task/ctf"
	"pdocker/types"

	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
)

// @Summary 创建Ctf 任务
// @Tags task/ctf
// @Description "创建ctf任务所需的所有参数(部分非必须)"
// @Accept json
// @Produce json
// @Param task body ctf.TaskCreateModel true "创建ctf任务参数"
// @Failure 400 {object} types.TaskResponse "{"code":400,"taskid":"","msg":"格式错误"}"
// @Failure 403 {object} types.TaskResponse "{"code":403,"taskid":"abcd123456","msg":"创建失败"}"
// @Failure 409 {object} types.TaskResponse "{"code":409,"taskid":"abcd123456","msg":"任务已存在"}"
// @Success 200 {object} types.TaskResponse "{"code":200,"taskid":"abcd123456","msg":"创建成功"}"
// @Router /task/ctf [post]
func CtfTaskCreate(c *gin.Context) {
	model := new(ctf.TaskCreateModel)
	err := c.BindJSON(model)
	if err != nil {
		simpleReturn(c, http.StatusBadRequest, "", "格式错误")
	} else {
		t := ctf.Task{
			Task: task.Task{
				UID:              model.UID,
				TaskId:           model.TaskId,
				DestroyTimeStamp: model.DestroyTimeStamp,
				CallBack:         model.CallBack,
			},
			CtfDocker: docker.CtfDocker{
				Docker: docker.Docker{
					Image: model.Image,
					Ports: model.Ports,
				},
				Flags: model.Flags,
			},
		}
		var control types.TaskControl = &t
		err = control.Create()
		if err != nil {
			errMsg := fmt.Sprintf("创建失败  %s", err)
			log.Error(errMsg)
			simpleReturn(c, http.StatusForbidden, t.Task.TaskId, errMsg)
		} else {
			var table types.TaskTable = &ctf.TaskTable{TaskId: model.TaskId, Data: t}
			ok := table.Insert()
			log.Debugf("table.insert() return %+v", ok)
			if ok {
				simpleReturn(c, http.StatusCreated, t.Task.TaskId, "创建成功")
				go control.Start()
			} else {
				simpleReturn(c, http.StatusConflict, t.Task.TaskId, "任务已存在")
			}

		}
	}

}

// @Summary 更新Ctf任务
// @Tags task/ctf
// @Description "重新指定销毁时间"
// @Accept json
// @Produce json
// @Param task body ctf.TaskUpdateModel true "根据销毁时间戳倒计时销毁"
// @Failure 400 {object} types.TaskResponse "{"code":400,"taskid":"","msg":"格式错误"}"
// @Failure 403 {object} types.TaskResponse "{"code":403,"taskid":"","msg":"任务不存在"}"
// @Success 200 {object} types.TaskResponse "{"code":200,"taskid":"abcd123456","msg":"更新成功"}"
// @Router /task/ctf [put]
func CtfTaskUpdate(c *gin.Context) {
	model := new(ctf.TaskUpdateModel)
	err := c.BindJSON(model)
	if err != nil {
		simpleReturn(c, http.StatusBadRequest, "", "格式错误")
	} else {
		ctfTask := ctf.Task{
			Task: task.Task{
				TaskId:           model.TaskId,
				DestroyTimeStamp: model.DestroyTimeStamp,
			},
		}
		var control types.TaskControl = &ctfTask
		var table ctf.TaskTable = ctf.TaskTable{Data: ctfTask}
		ok := table.Select()
		if ok {
			go func() {
				table.Update(model.DestroyTimeStamp)
				control.Update(model.DestroyTimeStamp)
			}()
			simpleReturn(c, http.StatusOK, ctfTask.Task.TaskId, "更新成功")
		} else {
			simpleReturn(c, http.StatusForbidden, ctfTask.Task.TaskId, "任务不存在")
		}
	}

}

// @Summary 销毁Ctf任务
// @tags task/ctf
// @Description "立刻销毁Ctf任务"
// @Accept json
// @Produce json
// @Param task body ctf.TaskDeleteModel true "立即销毁当前任务"
// @Success 200 {object} types.TaskResponse "{"code":200,"taskid":"abcd123456"}"
// @Router /task/ctf [delete]
func CtfTaskDelete(c *gin.Context) {
	model := new(ctf.TaskDeleteModel)
	err := c.BindJSON(model)

	if err != nil {
		simpleReturn(c, http.StatusBadRequest, "", "格式错误")
	} else {
		ctfTask := ctf.Task{
			Task: task.Task{
				TaskId: model.TaskId,
			},
		}
		var table types.TaskTable = &ctf.TaskTable{Data: ctfTask}
		ok := table.Select()
		log.Debugf("## delete table: %+v", table)

		//var control types.TaskControl = ctfTask.Task
		//log.Debugf("%+v", control)
		if ok {
			go func() {
				//		control.Delete()
				table.Delete()
			}()
			simpleReturn(c, http.StatusOK, ctfTask.Task.TaskId, "删除成功")
		} else {
			log.Warningf("任务id: %s 不存在\n", ctfTask.Task.TaskId)
			simpleReturn(c, http.StatusForbidden, ctfTask.Task.TaskId, "任务不存在")
		}
	}
}
